import math

from otree.api import (
    models,
    widgets,
    BaseConstants,
    BaseSubsession,
    BaseGroup,
    BasePlayer,
    Currency as c,
    currency_range,
)

doc = """
This test_bargaining game involves 2 players. Each demands for a portion of some
available amount. If the sum of demands is no larger than the available
amount, both players get demanded portions. Otherwise, both get nothing.
"""


class Constants(BaseConstants):
    name_in_url = 'S_bargaining2_second'
    # 2个人一组 进行bargaining测试
    players_per_group = 3
    # 进行无数轮(40)
    num_rounds = 50
    instructions_template = 'S_bargaining2_second/instructions.html'

    amount_shared = c(100)


class Subsession(BaseSubsession):
    # pass
    def creating_session(self):  # 分组 使用一开始建立的分组
        # matrix =[[self.session.vars['r1'],self.session.vars['r2']],[self.session.vars['b1'],self.session.vars['b2']]]
        # self.set_group_matrix(matrix)
        matrix = self.session.vars['second_contest']
        self.set_group_matrix(matrix)


class Group(BaseGroup):
    end_game = models.BooleanField(initial=False)
    total_requests = models.CurrencyField()
    point_pool = models.FloatField(initial=300)
    not_refer = models.BooleanField(initial=False)
    # percent=models.FloatField(initial=4) #默认值为第一轮？

    def set_payoffs(self):
        players = self.get_players()
        # 如果有人选择退出游戏 那么结束比赛 退出游戏的玩家获得64货币
        # 有人退出了 ->直接其他人0 False
        # 没有人退出 ->有人没提交False 都提交 True
        # 所有人都提交了
        for p in players:
            if p.quit_game == True:  # 有人退出了
                # 如果有人选择退出 使用如下分支 选择退出的获得64 不选择的获得0
                for player in players:
                    if player.quit_game == True:
                        player.payoff = 64
                        player.request = 0
                        # player.participant.vars['total_payoff'] = 0  # 第一次 赋给用户初值0
                        player.participant.vars['total_payoff'] += player.payoff  # 计算获得总金额
                    if player.quit_game == False:
                        player.payoff = 0
                        # player.participant.vars['total_payoff'] = 0  # 第一次 赋给用户初值0
                        player.participant.vars['total_payoff'] += player.payoff  # 计算获得总金额
                # 计算共同的需求相加 如果有人退出游戏变成0 相加自然不计算他的值
                self.total_requests = sum([player.request for player in players])
                self.end_game = True
                return
        for p in players:
            if p.not_refer == True:  # 有人没有提交
                # 所有没提交的人的数值变为0
                for player in players:
                    p.payoff = p.request
                    p.participant.vars['total_payoff'] += p.payoff  # 计算获得总金额
                    if player.not_refer == True:
                        p.payoff = 0
                        p.request = 0
                # 计算后返回false
                self.total_requests = sum([p.request for p in players])
                # 所有人的payoff为0
                for p1 in players:
                    p1.payoff = 0
                self.end_game = False
                return

        # 没人退出也没人提交
        # 计算共同的需求相加
        self.total_requests = sum([p.request for p in players])
        # 如果总共的钱小于等与as 那么结束比赛
        if self.total_requests <= self.point_pool:
            for p in players:
                p.payoff = p.request
                # p.participant.vars['total_payoff'] = 0  # 第一次 赋给用户初值0
                p.participant.vars['total_payoff'] += p.payoff  # 计算获得总金额
                # p.participant.vars['total_payoff'] 个人赚的钱
            self.end_game = True

        else:
            for p in players:
                p.payoff = c(0)
            self.end_game = False


    # 设置奖金池 随轮次减少
    def set_point_pool(self):
        if self.round_number == 1:
            self.point_pool = 300
        else:
            self.point_pool = self.point_pool - (self.round_number - 1) * 6  # round 保留两位
            # self.point_pool =round(100*math.pow(0.96, self.round_number - 1))# round 保留两位
            # self.point_pool=self.point_pool #用于页面展示的Point_pool
            # self.point_pool=self.point_pool #用于页面展示的Point_pool


class Player(BasePlayer):
    request = models.CurrencyField(
        doc="""
        Amount requested by this player.
        """, blank=True
    )
    quit_game = models.BooleanField(initial=False)
    not_refer = models.BooleanField(initial=False)

    # 给模板传数据
    def request_max(self):
        group = self.group
        return group.point_pool

    def other_player(self):  # 返回用户0的？？？
        # return self.get_others_in_group()[0]
        return self.get_others_in_group()

    def other_players_quit_game(self):  # 返回其他玩家的数组 两个人 所以为 0 和1
        # others=self.get_others_in_group()
        # for op in others:
        #     op.
        # self.
        return self.get_others_in_group()

    def previous_round_other_demand1(self):
        return self.get_others_in_group()[0].in_previous_rounds()

    def previous_round_other_demand2(self):
        return self.get_others_in_group()[1].in_previous_rounds()

    def game_round(self):
        number = self.round_number
        round = []
        for n in range(1, number):
            round.append(n)
        return round

    # def all_round_you_demand(self):
    #     pass
